package com.kgc.skProvider.service.serviceimpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.WriteOff;
import com.kgc.collection.pojo.WriteoffDetail;
import com.kgc.collection.pojo.WriteoffLink;
import com.kgc.collection.pojo.WriteoffType;
import com.kgc.skProvider.dao.WriteOffMapper;
import com.kgc.skProvider.service.WriteOffService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class WriteOffServiceImpl implements WriteOffService {

    private Logger logger = LoggerFactory.getLogger(WriteOffServiceImpl.class);


    @Autowired
    private WriteOffMapper writeOffMapper;

    @Override
    public ResponseMsg selectWriteOffType() {
        logger.info("WriteOffServiceImpl  selectWriteOffType are start");
        ResponseMsg responseMsg = new ResponseMsg();
        List<WriteoffType> writeOffTypeList = writeOffMapper.selectWriteOffType();
        responseMsg.setData(writeOffTypeList);
        logger.info("WriteOffServiceImpl  selectWriteOffType are end");
        return responseMsg;
    }

    @Override
    public ResponseMsg selectWriteOffByPage(Map<String, Object> map) {
        logger.info("WriteOffServiceImpl  selectWriteOffByPage params is {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        PageInfo<WriteOff> pageInfo =new PageInfo<>();
        pageInfo.setPageNo(map.get("pageNo")==null?1:Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize")==null?10:Integer.parseInt(map.get("pageSize").toString()));
        Page page= PageHelper.startPage(pageInfo.getPageNo(),pageInfo.getPageSize());
        List<WriteOff> writeOffList = writeOffMapper.selectWriteOffByPage(map);
        pageInfo.setList(writeOffList);
        pageInfo.setTotal((int)page.getTotal());
        responseMsg.setData(pageInfo);
        logger.info("WriteOffServiceImpl  selectWriteOffByPage is end return {}",responseMsg.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg insertWriteOff(Map<String, Object> map) {
        logger.info("WriteOffServiceImpl  insertWriteOff params is {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        ObjectMapper mapper = new ObjectMapper();
        WriteOff writeOff=mapper.convertValue(map.get("writeOff"),WriteOff.class);
        WriteoffLink writeoffLink=mapper.convertValue(map.get("writeoffLink"),WriteoffLink.class);
        List<?> rawDetails = (List<?>) map.get("details");
        List<WriteoffDetail> details = mapper.convertValue(rawDetails,new TypeReference<List<WriteoffDetail>>(){});
        int result = writeOffMapper.insertWriteOff(writeOff);
        int flag=writeOffMapper.insertWriteOffLink(writeoffLink);
        try{
            for(WriteoffDetail detail:details){
                writeOffMapper.insertWriteOffDetail(detail);
            }
            responseMsg.setCode("200");
        }catch(Exception e){
            e.printStackTrace();
            responseMsg.setCode("201");
            return responseMsg;
        }
        if(result>0&&flag>0){
            responseMsg.setCode("200");
        }else{
            responseMsg.setCode("201");
        }
        logger.debug("WriteOffServiceImpl  insertWriteOff result is {}",responseMsg.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg updateWriteOff(Map<String, Object> map) {
        logger.info("WriteOffServiceImpl  updateWriteOff params is {}",map.toString());
        deleteWriteOff(map);
        ResponseMsg responseMsg = insertWriteOff(map);
        logger.debug("WriteOffServiceImpl  updateWriteOff result is {}",responseMsg.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg deleteWriteOff(Map<String, Object> map) {
        logger.info("WriteOffServiceImpl  deleteWriteOff params is {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        int result1 = writeOffMapper.deleteWriteOff(map);
        int result2 = writeOffMapper.deleteWriteOffDetail(map);
        if(result1>0&&result2>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("WriteOffServiceImpl  deleteWriteOff result is {}",responseMsg.toString());
        return responseMsg;
    }

    @Override
    public ResponseMsg checkWriteOff(Map<String, Object> map) {
        logger.info("WriteOffServiceImpl  checkWriteOff params is {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        int result1 = writeOffMapper.checkWriteOff(map);
        if(result1>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("WriteOffServiceImpl  checkWriteOff result is {}",responseMsg.toString());
        return responseMsg;
    }

    @Override
    public ResponseMsg selectWriteOffByCode(Map<String, Object> map) {
        logger.info("WriteOffServiceImpl  selectWriteOffByCode params is {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        WriteOff writeOff = writeOffMapper.selectWriteOffByCode(map);
        List<WriteoffDetail> list=writeOffMapper.selectWriteOffDetail(map);
        writeOff.setDetails(list);
        responseMsg.setCode("200");
        responseMsg.setData(writeOff);
        logger.debug("WriteOffServiceImpl  selectWriteOffByCode result is {}",responseMsg.toString());
        return responseMsg;
    }
}
